<template>
	<view>
		<view class="top_mag_big">
			<u-input border="surround" placeholder="请输入您的账号" v-model="info.username" clearable />
			<u-input type="password" border="surround" placeholder="请输入您的密码" v-model="info.password" clearable />
			<button @click="userLogin()" type="primary">登录</button>
		</view>
		<u-gap height="30" bgColor="#f8f8f8"></u-gap>
		<view>
			<u--text type="primary" size='25' align='right' text="没有账号？点击注册" @click='toRegister'></u--text>
		</view>
	</view>
</template>

<script>
	import {
		login
	} from '@/config/api.js'
	export default {
		data() {
			return {
				info: {
					username: '',
					password: '',
					token: ''
				}

			}
		},
		methods: {
			//跳转到注册
			toRegister() {
				uni.navigateTo({
					url: 'register/register'
				})
			},
			/**
			 * 登录请求
			 */
			async userLogin() {
				if (this.info.username === '' && this.info.password === '') {
					uni.$u.toast("用户名或密码不能为空")
				} else {
					await login({
						username: this.info.username,
						password: this.info.password
					}).then((res) => {
						console.log('这是login里的方法，到这里说明成功了');
						uni.setStorage({
							key: 'user_token',
							data: res.token,
							success: function() {
								console.log('存储进入了t', res.token)
							}
						});
						console.log('存储进入了n', this.info.username);
						uni.setStorage({
							key: 'user_name',
							data: this.info.username,
							success: function() {
								console.log("用户姓名为" + res)
							}
						});
						uni.setStorage({
							key: 'is_login',
							data: 'yes',
							success: function() {
								console.log("成功设置登录信息接口为yes");
							}
						});
						//路由跳转之后传值过去
						uni.switchTab({
							url: `/pages/mine/mine`
						});

					}).catch(() => {

					})

				}
			}
		},
	}
</script>

<style>
	.top_mag_big {
		margin-top: 50rpx;
	}
</style>
